#ifndef __GND_SERVICE_CONNECTION_H
#define __GND_SERVICE_CONNECTION_H

#include "Service.h"
#include "Global.h"

#include <sigc++/slot.h>
#include <sigc++/signal.h>
#include <memory>
#include <list>
#include <thread>

#include <glibmm/main.h>

namespace Gnodroid {

    class ServiceConnection : public Service
    {
    public:
        //typedef sigc::signal<void, std::string> TypeSignalConnect;

        ServiceConnection();
      
        //asycn method
        //bool getDeviceList(sigc::slot<bool, std::shared_ptr<std::list<DeviceInfo> > >);
        bool
        startServerAsyn(sigc::slot<bool, bool> slotCb);
        //asycn by signal
        bool
        connect(std::string id);
        bool
        disconnect();
        //要求线程安全
        bool
        isConnecting();
        
        
        //sycn
        bool 
        checkAdb(std::string path = "");

        std::string 
        getAdbPath();

        std::string
        getDeviceId();

        bool 
        setAdbPath(std::string path);

        int
        getAnPort();
        int
        getPcPort();

        std::shared_ptr<std::list<DeviceInfo> >
        getDeviceList();

        bool
        startServer();

        //在任意线程调用，信号在主线程发送
        void
        emitSignalConnected();          //连接成功
        void
        emitSignalConnectFailed();      //连接失败
        void
        emitSignalDisconnected();       //断开连接


        sigc::signal<void, std::string>
        signal_connected();

        sigc::signal<void, std::string>
        signal_connectfailed();

        sigc::signal<void, std::string>
        signal_disconnected();

        void start();
        void stop();

    private:
        std::string m_strAdbPath {"/bin/adb"};
        int m_iPcPort {10086};
        int m_iAnPort {10086};
        bool m_bIsConnecting {false};
        std::string m_strDeviceId;

        Glib::RefPtr<Glib::MainContext> m_rpMainContext {nullptr};
        Glib::RefPtr<Glib::MainLoop> m_rpMainLoop {nullptr};


        sigc::signal<void, std::string> m_signalConnected;
        sigc::signal<void, std::string> m_signalConnectFailed;
        sigc::signal<void, std::string> m_signalDisconnected;

        //运行在后台线程进行连接建立和状态维持检测
        void __connectThread(std::string id);


    };

}


#endif
